/*=====================================================================================

 news_narrative.do: 
	
   Creates the expected dividend based on the narrative analysis in APF_Data.xlsx
 

		Lorenz Kueng, Feb 2015

=======================================================================================*/

cap log close PFD_02
log using "$homedir/log-files/PFD_02_$date.log", replace name(PFD_02)					


*===================================================================
* News Shock: Expected next dividend based on narrative analysis
*===================================================================

import excel "$homedir/data/APF_Data.xlsx", sheet("4. narrative analysis") cellrange(B3:I232) firstrow clear

	generate date = mdy(month,day,year)
	format date %td
	order  date

	* rename variables 

	rename  bymail  by_mail
	rename  directdeposit direct_deposit
	rename  Aannouncement announcement 
	rename  Qquantitative narr_quant
	rename  QLqualitative narr_qual

	lab var by_mail        "date dividend checks were sent out by mail"
	lab var direct_deposit "date dividend was distributed via direct deposit"
	lab var announcement   "annoucement date of dividend amount"
	lab var narr_quant     "quantitative dividend news in media (narrative approach)"
	lab var narr_qual      "qualitative dividend news in media (narrative approach)"

	
    *===================================================================
	* quantitative narratives
    *===================================================================

	* keep only events with quantitative news
	
	keep if   narr_quant!=. | announcement!=.
	keep date narr_quant announcement
	replace narr_quant = announcement if narr_quant==. // the dividend announcement is a news event

	
	*------------------------------------------------------
	* Construct expected dividend for figure
	*------------------------------------------------------

	cap drop dateM
	generate dateM = mofd(date)
	format   dateM %tm
	sort     dateM

	collapse (mean) announcement narr_quant, by(dateM)

	replace announcement=. if dateM==m(2008m8) // drop announcement date of $1,200 energy rebate

	replace narr_quant = announcement if narr_quant!=announcement & announcement!=.

	tempfile narr_quant
	save `narr_quant'
	
	// make balanced panel (instead of using tsfill)
	use date using "$homedir/data/stata/calendar_days.dta", clear
	generate  dateM = mofd(date)
	format    dateM %tm
	keep      dateM
	duplicates drop dateM, force
	merge 1:1 dateM using `narr_quant'
	drop _merge

	drop if dateM<m(1983m1)
	drop if dateM>m(2014m9)
	
	cap drop month
	generate month = month( dofm(dateM) )
	
	cap drop exp_div
	generate exp_div = narr_quant
	replace  exp_div = 0.01 if announcement!=.
	replace  exp_div = exp_div[_n-1] if exp_div==.
	replace  exp_div = . if round(exp_div)==0
	replace  exp_div = . if announcement!=.
	lab var  exp_div "expected PFD (narrative-based)"
	
	generate PFDactual = announcement
	forvalues i=1(1)12 {
		replace  PFDactual = PFDactual[_n+1] if PFDactual==.
	}
	lab var  PFDactual "actual Permanent Fund Dividend (PFD) "

	generate Month = dateM
	format   Month %tm
	
	twoway connect exp_div      Month, lc(black) lw(thin)  m(i) mlw(thin) mc(black) connect(stepstair) cmissing(no) || ///
		   connect PFDactual    Month, lc(blue)  lw(vthin) m(i) mlw(thin) mc(blue)  connect(stepstair) lp(shortdash) || ///	  		   
		   scatter announcement Month, lw(vthin) m(o) msize(small) mlw(thin) mc(blue)   || ///	  		   
		   , xtitle("") ytitle("dollars") yscale(r(0)) ylabel(0(500)3200) graphregion(color(white)) legend( position(10) ring(0) order(2 1) rows(2) region(lwidth(none)) ) 

		local file "$homedir/results/figures/dividend_vs_expectation_narratives"
			graph export "`file'.eps", replace
			cap rm       "`file'.pdf"
			!epstopdf    "`file'.eps"
			graph export "`file'.tif", replace


	*------------------------------------------------------
	* Assess forecast error
	*------------------------------------------------------

	cap drop  ForecastError
	generate  ForecastError = PFDactual - exp_div
	summarize ForecastError, detail
	summarize ForecastError if Month<m(2008m1) | Month>m(2008m5), detail // exclude error due to Alaska Resource Rebate (to make forecast error comparable to the market-based forecast error)				

log close PFD_02
